<!--
  * 娃娃相册查询
  *
  * @Author:    kaiyun
  * @Date:      2024-06-23 23:31:38
  * @Copyright  <a href="https://1024lab.net">1024创新实验室</a>
-->
<template>
  <!---------- 查询表单form begin ----------->  
  <a-form class="smart-query-form">
    <a-row class="smart-query-form-row">
      <a-form-item label="订单编号" class="smart-query-form-item">
        <a-input style="width: 200px" v-model:value="queryForm.orderNo" placeholder="请输入娃娃ID" />
      </a-form-item>
      <a-form-item label="创建时间" class="smart-query-form-item">
        <a-range-picker v-model:value="createDateRange" :presets="defaultTimeRanges" style="width: 240px" @change="changeCreateDate" />
      </a-form-item>
      <a-form-item class="smart-query-form-item">
        <a-button-group>
          <a-button type="primary" @click="onSearch">
            <template #icon>
              <SearchOutlined />
            </template>
            查询
          </a-button>
          <a-button @click="resetQuery">
            <template #icon>
              <ReloadOutlined />
            </template>
            重置
          </a-button>
        </a-button-group>
      </a-form-item>
    </a-row>
  </a-form>
  <!---------- 查询表单form end ----------->  

  <a-card size="small" :bordered="false" :hoverable="true">
    <!---------- 表格 begin ----------->  
    <a-table
      :columns="columns"
      :data-source="tableData"
      :loading="tableLoading"
      :pagination="{
        total: total,
        current: queryForm.pageNum,
        pageSize: queryForm.pageSize,
        showSizeChanger: true,
        showQuickJumper: true,
        showTotal: (total) => `共 ${total} 条`,
        onChange: changePage,
      }"
      :scroll="{ x: 800 }"
      row-key="dollNo"
    >
      <template #bodyCell="{ column, record }">
        <template v-if="column.dataIndex === 'imageUrl'">
          <a-image
            :src="record.imageUrl"
            :preview="true"
            :width="80"
            :height="80"
          />
        </template>
      </template>
    </a-table>
    <!---------- 表格 end ----------->  
  </a-card>
</template>

<script setup>
  import { ref, reactive, onMounted,watch } from 'vue';
  import { useRoute } from 'vue-router';
  
  import { SearchOutlined, ReloadOutlined } from '@ant-design/icons-vue';
  import { defaultTimeRanges } from '/@/lib/default-time-ranges';
  import { message } from 'ant-design-vue';
  import { userPhotosApi } from '/@/api/business/bjd/user-photos-api';

  // --------------------------- 表格列定义 ---------------------------
  const columns = ref([
    {
      title: '娃娃编号',
      dataIndex: 'orderNo',
      width: 150,
      ellipsis: true,
    },
    {
      title: '图片',
      dataIndex: 'imageUrl',
      width: 100,
    },

    {
      title: '创建时间',
      dataIndex: 'createTime',
      width: 150,
    },
  ]);

  // --------------------------- 查询相关 ---------------------------
  const queryFormState = {
    dollNo: undefined,
    createTimeBegin: undefined,
    createTimeEnd: undefined,
    pageNum: 1,
    pageSize: 10,
  };

  const queryForm = reactive({ ...queryFormState });
  const tableLoading = ref(false);
  const tableData = ref([]);
  const total = ref(0);
  const createDateRange = ref([]);
  const route = useRoute();

  // 查询数据
  function onSearch() {
    queryForm.pageNum = 1;
    queryData();
  }

  // 重置查询
  function resetQuery() {
    createDateRange.value = [];
    Object.assign(queryForm, queryFormState);
    queryData();
  }
   // 监听路由参数变化
   watch(
    () => route.query,
    (newQuery) => {
      const orderNo = newQuery.orderNo;
      console.log('orderNo',orderNo)
      if (orderNo) {
        queryForm.orderNo = orderNo;
        queryData();
      }
    },
    { immediate: true } // 立即执行一次
  );

  // 分页改变
  function changePage(page, pageSize) {
    queryForm.pageNum = page;
    queryForm.pageSize = pageSize;
    queryData();
  }

  // 创建时间改变
  function changeCreateDate(dates) {
    if (dates && dates.length === 2) {
      queryForm.createTimeBegin = dates[0];
      queryForm.createTimeEnd = dates[1];
    } else {
      queryForm.createTimeBegin = undefined;
      queryForm.createTimeEnd = undefined;
    }
  }

  // 查询数据方法
  async function queryData() {
    try {
      tableLoading.value = true;
      const result = await userPhotosApi.queryPage(queryForm);
      tableData.value = result.data.list;
      total.value = result.data.total;
    } catch (err) {
      console.error(err);
      message.error('查询失败');
    } finally {
      tableLoading.value = false;
    }
  }

  onMounted(() => {


    // 初始加载数据，watch 中的 immediate: true 已经处理了 URL 参数
    if (!route.query.orderNo) {
      queryData();
    }
  });
</script>